﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CXMLStudioIDE.Common.Entities;
using CXMLStudioIDE.DataAccessLayer;

namespace CXMLStudioIDE.Forms
{
    public partial class AddNewEntityForm : Form
    {
        #region Declarations
        private MainForm frm_parent;
        private int m_selected_project;
        private Project m_project;
        private CXMLStudioDAL m_dal = null;
        private bool b_inserted = false;
        #endregion

        #region Constructors
        public AddNewEntityForm(Form parent, int i_TreeViewID)
        {
            InitializeComponent();
            frm_parent = parent as MainForm;
            m_dal = new CXMLStudioDAL();
            InitComboBoxes();
            m_selected_project = i_TreeViewID;
            m_project = frm_parent.m_SolutionManager.m_solution.Projects.GetProjectByTreeviewID(m_selected_project);
        }
        #endregion
        private void InitComboBoxes()
        {
            dsContext = m_dal.GetContext();
            cmbContext.DisplayMember = dsContext.Tables[0].Columns[1].ColumnName.ToString();
            cmbContext.ValueMember = dsContext.Tables[0].Columns[0].ColumnName.ToString();
            cmbContext.DataSource = dsContext.Tables[0];
            dsEntityType = m_dal.GetEntityTypes();
            cmbEntityType.DisplayMember = dsEntityType.Tables[0].Columns[1].ColumnName.ToString();
            cmbEntityType.ValueMember = dsEntityType.Tables[0].Columns[0].ColumnName.ToString();
            cmbEntityType.DataSource = dsEntityType.Tables[0];
            dsEntityVersion = m_dal.GetEntityVersions();
            cmbEntityVersion.DisplayMember = dsEntityVersion.Tables[0].Columns[1].ColumnName.ToString();
            cmbEntityVersion.ValueMember = dsEntityVersion.Tables[0].Columns[0].ColumnName.ToString();
            cmbEntityVersion.DataSource = dsEntityVersion.Tables[0];
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (b_inserted)
            {
                frm_parent.UpdateMainForm();
            }
            this.Dispose();
        }

        private void cmbEntityVersion_SelectedIndexChanged(object sender, EventArgs e)
        {
            string EntityVersion = cmbEntityVersion.Text;
            if (EntityVersion != null && EntityVersion.Length > 0)
            {
                EntityVersion ev = Common.Common.ParseEntityVersion(EntityVersion);
                if (ev != null)
                {
                    txtMajorVersion.Text = ev.MajorVersion;
                    txtMinorVersion.Text = ev.MinorVersion;
                    txtDataValuesVersion.Text = ev.DataValuesVersion;
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DataSet dsResult = null;
            if (cmbContext.Text.Length > 0 && txtEntityType.Text.Length > 0 &&
                txtMajorVersion.Text.Length > 0 && txtMinorVersion.Text.Length > 0 &&
                txtDataValuesVersion.Text.Length > 0 && txtEntityCode.Text.Length > 0)
            {
                this.Cursor = Cursors.WaitCursor;
                int isbe = (chkIsBusinessEntity.Checked ? 1 : 0);
                dsResult = m_dal.SaveContextualVersionEntity(cmbContext.Text, txtEntityType.Text,
                    txtMajorVersion.Text, txtMinorVersion.Text, txtDataValuesVersion.Text,
                    txtEntityCode.Text, isbe);
                if (dsResult != null && dsResult.Tables.Count > 0 && dsResult.Tables[0].Rows.Count > 0)
                {
                    txtEntityCode.Clear();
                    txtEntityCode.Text = "New Entity Creation Report:\r\n";
                    txtEntityCode.Text += "___________________________\r\n";
                    BusinessEntity be = new BusinessEntity();
                    foreach (DataRow row in dsResult.Tables[0].Rows)
                    {
                        be.EntityType.EntityTypeID = (int)row["EntityTypeID"];
                        be.EntityTypeVersions.EntityTypeVersionsID = (int)row["EntityTypeVersionID"];
                        txtEntityCode.Text += "\r\n EntityID............:" + row["EntityID"];
                        txtEntityCode.Text += "\r\n EntityTypeID........:" + row["EntityTypeID"];
                        txtEntityCode.Text += "\r\n EntityTypeName......:" + txtEntityType.Text;
                        txtEntityCode.Text += "\r\n EntityContextName...:" + cmbContext.Text;
                        txtEntityCode.Text += "\r\n EntityTypeVersionID.:" + row["EntityTypeVersionID"];
                        txtEntityCode.Text += "\r\n EntityVersion.......:" + txtMajorVersion.Text + "." + txtMinorVersion.Text + "." + txtDataValuesVersion.Text;
                        txtEntityCode.Text += "\r\n StatusCode..........:" + row["StatusCode"];
                        txtEntityCode.Text += "\r\n StatusDescription...:" + row["StatusDescription"];
                    }
                    btnCancel.Text = "Close";
                    btnOK.Visible = false;
                    be.ParentProject = m_project;
                    be.Name = txtEntityType.Text;
                    be.EntityVersion.FullVersion = txtMajorVersion.Text + "." + txtMinorVersion.Text + "." + txtDataValuesVersion.Text;
                    this.Cursor = Cursors.WaitCursor;
                    if (frm_parent.m_SolutionManager.m_solution != null && frm_parent.m_SolutionManager.m_solution.Projects != null)
                    {
                        frm_parent.m_SolutionManager.AddEntityToProject(m_project.ID, be);
                        b_inserted = true;
                    }
                }
                this.Cursor = Cursors.Default;
            }
            else
            {
                MessageBox.Show(this, "Please select Context, EntityType name, Version and enter Entity Code", "CXMLStudioIDE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void cmbEntityType_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtEntityType.Text = cmbEntityType.Text;
        }

        private void txtMajorVersion_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                    e.Handled = true;
            }
        }

        private void txtMinorVersion_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                    e.Handled = true;
            }
        }

        private void txtDataValuesVersion_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                    e.Handled = true;
            }
        }

        private void txtMinorVersion_Leave(object sender, EventArgs e)
        {
            if (txtMinorVersion.Text.Length > 0 && txtMinorVersion.Text.Length < 3)
            {
                txtMinorVersion.Text = txtMinorVersion.Text.PadLeft(3, '0');
            }
        }

        private void txtDataValuesVersion_Leave(object sender, EventArgs e)
        {
            if (txtDataValuesVersion.Text.Length > 0 && txtDataValuesVersion.Text.Length < 3)
            {
                txtDataValuesVersion.Text = txtDataValuesVersion.Text.PadLeft(3, '0');
            }
        }

    }
}
